package com.see.domain.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.see.domain.model.StaticData;

public class StaticDataDaoImpl implements StaticDataDao {
    private final PersistenceManager persistenceManager;

    public StaticDataDaoImpl() {
        this.persistenceManager = PMF.get().getPersistenceManager();
    }

    @Override
    @Transaction
    public void insert(StaticData staticData) {
        persistenceManager.makePersistent(staticData);
    }

    @Override
    @Transaction
    public void update(String symbol, StaticData staticData) {
        StaticData uptStaticData = persistenceManager.getObjectById(StaticData.class, symbol);
        uptStaticData.setSector(staticData.getSector());
        uptStaticData.setIndustry(staticData.getIndustry());
        uptStaticData.setCompanyName(staticData.getCompanyName());
        persistenceManager.makePersistent(uptStaticData);
    }

    @Override
    @Transaction
    public void delete(String symbol) {
        StaticData delStaticData = persistenceManager.getObjectById(StaticData.class, symbol);
        persistenceManager.deletePersistent(delStaticData);
    }

    @Override
    public StaticData[] findAll() {
        Query query = persistenceManager.newQuery(StaticData.class);
        //Extent<StaticData> e = persistenceManager.getExtent(StaticData.class, true);
        @SuppressWarnings("unchecked")
        List<StaticData> list = (List<StaticData>) query.execute();
        return list.toArray(new StaticData[list.size()]);
    }

    @Override
    public StaticData findByPrimaryKey(String symbol) {
        StaticData staticData = persistenceManager.getObjectById(StaticData.class, symbol);
        return staticData;
    }

    @Override
    public StaticData[] findBySector(String sector) {
        Query query = persistenceManager.newQuery(StaticData.class, "sector == sector");
        //query.setFilter("sector == sector");
        query.declareParameters("String sector");
        @SuppressWarnings("unchecked")
        List<StaticData> staticDatas = (List<StaticData>) query.execute(sector);

        // commons collections
        if (staticDatas == null || staticDatas.size() == 0) {
            return new StaticData[] {};
        }
        return staticDatas.toArray(new StaticData[staticDatas.size()]);
    }

    @Override
    public StaticData[] findByIndustry(String industry) {
        Query query = persistenceManager.newQuery(StaticData.class, "industry == industry");
        //query.setFilter("industry == industry");
        query.declareParameters("String industry");
        @SuppressWarnings("unchecked")
        List<StaticData> staticDatas = (List<StaticData>) query.execute(industry);

        // commons collections
        if (staticDatas == null || staticDatas.size() == 0) {
            return new StaticData[] {};
        }
        return staticDatas.toArray(new StaticData[staticDatas.size()]);
    }
}
